#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LIC_PID_LEN     11
#define LIC_SN_LEN      22
#define LIC_UDI_LEN     LIC_PID_LEN + LIC_SN_LEN

typedef struct licensable_udi_ {
    char pid[LIC_PID_LEN + 1];
    char sn[LIC_SN_LEN + 1];
} licensable_udi_t;

static int
test_free(void)
{
    char *aaa = NULL;
    char *a;
    char *b = NULL;

    printf("\n%s(): %d - ", __FUNCTION__, __LINE__);

    /* a's value is random */
    printf("\n      %d - a 0x%x", __LINE__, a);
    a = malloc(12);
    if (a) {
        printf("\n      %d - a 0x%x", __LINE__, a);
        *a = "aaa";
        printf("\n      %d - *a %s", __LINE__, a);
        *a = 'a';
        printf("\n      %d - *a %s", __LINE__, a);
        //*a = *b;      /* crash here, can't dereference NULL */
        printf("\n      %d - *a %s", __LINE__, a);
    }
    printf("\n      %d - ", __LINE__);

    free(aaa);

    printf("\n      %d - b %s", __LINE__, b);

    return 0;
}

static int
test_sizeof (void)
{
    char udistr[LIC_UDI_LEN + 1];

    printf("\n%4d - %s(): ", __LINE__,  __FUNCTION__);

    printf("\n%4d - sizeof(udistr) %d", __LINE__, sizeof(udistr));

    return 0;
}

int
main(char *argc, char *argv[])
{
    licensable_udi_t lic_udi, *lic_udi_to;

    strncpy(lic_udi.pid, "UBR-PRE5", LIC_PID_LEN);
    strncpy(lic_udi.sn, "CAT165E0985", LIC_SN_LEN);

    lic_udi_to = malloc(sizeof(*lic_udi_to));
    if (!lic_udi_to) {
        return -1;
    }

    memcpy(lic_udi_to, &lic_udi, LIC_UDI_LEN);

    printf("\n     %d - lic_udi_to", __LINE__);
    printf("\n     %d - ->pid %s  ", __LINE__, lic_udi_to->pid);
    printf("\n     %d - ->sn  %s  ", __LINE__, lic_udi_to->sn);

    test_free();

    test_sizeof();

    printf("\n\n");

    return 0;
}

